<?php
class Application_Model_ObjectMapper{

	protected $_dbTable;

	public function setDbTable($dbTable)
	{
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}

	public function getDbTable()
	{
		if (null === $this->_dbTable) {
			$this->setDbTable('Application_Model_DbTable_Object');
		}
		return $this->_dbTable;
	}
	
	/**
	 * Insert the object in the table 
	 * @param Application_Model_Object $object
	 * @return Ambigous <mixed, multitype:>|number
	 */
	public function save(Application_Model_Object $object)
	{
		$data = array(
				'name'   => $object->getName(),
				'description' => $object->getDescription(),
				'url' => $object->getUrl(),
		);
	
		if (null === ($id = $object->getObjectId())) {
			unset($data['id_object']);
			$id_object =  $this->getDbTable()->insert($data);
			
			//Add images
			foreach($object->getImages() as $image){
				$imagemapper = new Application_Model_ImageMapper();
				$id_image = $imagemapper->save($image);
				
				$assocValues = array(
						'imageId'	=> $id_image,
						'objectId'	=> $id_object,
				);
				$assocIO = new Application_Model_AssocObjImg($assocValues);
				$assocIOMapper = new Application_Model_AssocObjImgMapper();
				$assocIOMapper->save($assocIO);
			}
			
			return $id_object;
						
			
		} else {
			//TODO update
			$this->getDbTable()->update($data, array('id_object = ?' => $id));
			
			//Add images
			foreach($object->getImages() as $image){
				$imagemapper = new Application_Model_ImageMapper();
				$id_image = $imagemapper->save($image);
			
				$assocValues = array(
						'imageId'	=> $id_image,
						'objectId'	=> $id,
				);
				$assocIO = new Application_Model_AssocObjImg($assocValues);
				$assocIOMapper = new Application_Model_AssocObjImgMapper();
				$assocIOMapper->save($assocIO);
			}
			
			return $id;
		}
	}
	
	/**
	 * find the object in the table with the id
	 * @param int $id
	 * @param Application_Model_Object $object
	 * @return void|Application_Model_Object
	 */
	public function find($id, Application_Model_Object $object)
	{
		$assocObjImgMapper = new Application_Model_AssocObjImgMapper();
		$result = $this->getDbTable()->find($id);
		if (0 == count($result)) {
			return;
		}
		$row = $result->current();
		$object->setObjectId($row->id_object);
		$object->setName($row->name);
		$object->setDescription($row->description);
		$object->setUrl($row->url);
		$object->setImages($assocObjImgMapper->findImages($row->id_object));
		return $object;
	}
	
	public function fetchAll()
	{
		$resultSet = $this->getDbTable()->fetchAll();
		$entries   = array();
		foreach ($resultSet as $row) {
			$entry = new Application_Model_Address();
			$entry->setId($row->id_object)
			->setName($row->name)
			->setDescription($row->description)
			->setUrl($row->url);
			$entries[] = $entry;
		}
		return $entries;
	}
	
	public function delete($id)
	{
		
		$result = $this->getDbTable()->find($id);
		if (0 != count($result)) {
			$imageMapper = new Application_Model_ImageMapper();
			$objimg = new Application_Model_AssocObjImgMapper();
			$images = $objimg->findImages($id);
			$objimg->delete(array('id_object = ?' => $id));
			if(0 != count($images)){
				foreach ($images as $image){
					$imageMapper->delete($image->getId());
					if(file_exists(APPLICATION_PATH.DIRECTORY_SEPARATOR.$image->getUrl()))
						unlink(APPLICATION_PATH.DIRECTORY_SEPARATOR.$image->getUrl());
				}
				
			}
			$this->getDbTable()->delete(array('id_object = ?' => $id));
		}
	
	}
	
	public function indexBuilder(Zend_Search_Lucene_Document $doc, Application_Model_Object $object)
	{
		$doc->addField(Zend_Search_Lucene_Field::UnIndexed('objectId', $object->getObjectId(), 'utf-8'));
		$doc->addField(Zend_Search_Lucene_Field::Text('name', $object->getName(), 'utf-8'));
		$doc->addField(Zend_Search_Lucene_Field::Text('objectDescription', $object->getDescription(), 'utf-8'));
	
		return $doc;
	}
}